#include<graphics.h>
#include<conio.h>
#include<string.h>
#include<easyx.h>
#pragma comment(lib,"Winmm.lib")


//函数外全局变量定义
int bird_x,bird_y;//鸟的位置
int bar_x,bar_y1,bar_y2;//障碍物的位置
IMAGE img_bk,img_bd1,img_bd2,img_bar_up1,img_bar_up2,img_bar_down1,img_bar_down2;
HWND hwnd;
int exitflag = 0; //退出标记

void startup()//数据初始化
{     
	mciSendString("open E:\\background.mp3 alias bkmusic",NULL,0,NULL);//背景音乐
	mciSendString("play bkmusic repeat",NULL,0,NULL);//循环播放
	initgraph(350,600);
	loadimage(&img_bk,"E:\\background.jpg");//读取图片
	loadimage(&img_bd1,"E:\\bird1.jpg");
	loadimage(&img_bd2,"E:\\bird2.jpg");
	loadimage(&img_bar_up1,"E:\\bar_up1.gif");
	loadimage(&img_bar_up2,"E:\\bar_up2.gif");
	loadimage(&img_bar_down1,"E:\\bar_down1.gif");
	loadimage(&img_bar_down2,"E:\\bar_down2.gif");
	bird_x=50;
	bird_y=200;
	bar_x=300;
	bar_y1=-300;
	bar_y2=400;
	BeginBatchDraw();

}

void show()//显示画面
{
	putimage(0,0,&img_bk);//在坐标（0,0）处显示背景
	putimage(bar_x,bar_y1,&img_bar_up1,NOTSRCERASE);
	putimage(bar_x,bar_y1,&img_bar_up2,SRCINVERT);//显示上一半的障碍物
	putimage(bar_x,bar_y2,&img_bar_down1,NOTSRCERASE);
	putimage(bar_x,bar_y2,&img_bar_down2,SRCINVERT);//显示下一半的障碍物
	putimage(bird_x,bird_y,&img_bd1,NOTSRCERASE);
	putimage(bird_x,bird_y,&img_bd2,SRCINVERT);//显示小鸟
	FlushBatchDraw();
	Sleep(50);
}

void updateWithoutInput()//与用户输入无关的更新
{
   if(bird_y<580)
	   bird_y=bird_y+3;
   if(bird_x==bar_x-20&&(bird_y<bar_y1+600||bird_y>bar_y2))
	{
		MessageBox(hwnd, TEXT("你输了！"), _T("结果"), MB_OK);
			system("pause");
			exitflag = 1;
	}
	else
	{
		bar_x=bar_x-1;
	}
	if(bar_x+90<0)
	{
		bar_x=370;
		bar_y1=rand()%100-300;
		bar_y2=bar_y1+700;
	}
}


void updateWithInput()//与用户输入有关的更新
{
    char input;
	if(kbhit())//当按键时执行
	{
		    input=getch();
			if(input==' '&&bird_y>20)
				bird_y=bird_y-60;
			mciSendString("close jpmusic",NULL,0,NULL);
			mciSendString("open E:\\Jump.mp3 alias jpmusic",NULL,0,NULL);
			mciSendString("play jpmusic",NULL,0,NULL);


	}
}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}


int main()
{
	startup();//数据初始化
	while(1)//游戏循环执行
	{
		show();//显示画面
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
		if (exitflag) break;
	    
	}
	gameover();
	return 0;
}
